#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Implementation w/o BigNumbers
// When p is inf, change it to 0.

// Number of iterations
#define N 1000000
#define PRINT_PRECISION 6

int main(int argc, char const *argv[]){

	int i;

	double aCur, aLast = 1.0;
	double bCur, bLast = sqrt(2.0)/2.0;
	double tCur = 0.25;
	double pCur = 1.0;

	bool pInf = false;


	for (int i = 0; i < N; ++i){
		// Calculate new a
		aCur = (aLast + bLast)/2;

		// Calculate new b
		bCur = sqrt(aLast*bLast);

		// Calculate new t
		tCur = tCur - pCur*(aLast - aCur)*(aLast - aCur);

		// Calculate new p if p is not Inf yet
		if(!pInf){
			pCur*=2;
			if(isinf(pCur)){
				pInf = true;
				pCur = 0;
			}
		}

		// Save variables for next iteration
		aLast = aCur;
		bLast = bCur;
	}

	printf("a = %lf\nb = %lf\nt = %lf\np = %lf\n", aCur, bCur, tCur, pCur);

	// Print PI
	//gmp_printf("%.*Ff\n",PRINT_PRECISION,aCur);
	printf("%.*lf\n", PRINT_PRECISION, ((aCur + bCur)*(aCur + bCur))/(4*tCur));

	return 0;
}